﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AddIn.EA.Architecture.ComponentLogic;
using AddIn.EA.Architecture.Presentation.Interfaces;

namespace AddIn.EA.Architecture.Presentation.Controls
{
    public partial class BETreeView : UserControl, IAddNewBusinessEntity
    {
        private BusinessEntity _entity;

        public BETreeView()
        {
            InitializeComponent();
            InitializeView();
        }

        private void UpdateTreeView(BusinessEntity entity)
        {
            tvBE.Nodes.Clear();

            if (entity != null)
            {
                TreeNode classNode = new TreeNode();
                if (!String.IsNullOrEmpty(entity.Name))
                    classNode.Text = entity.Name;
                else
                    classNode.Text = string.Format("<New {0}>", entity.Stereotype);
                classNode.ImageIndex = 0;
                classNode.SelectedImageIndex = 0;

                if (entity.Properties != null && entity.Properties.Count > 0)
                {
                    foreach (Property property in entity.Properties)
                    {
                        TreeNode methodNode = new TreeNode();
                        if (!String.IsNullOrEmpty(property.Name))
                            methodNode.Text = string.Format("{0} : {1} {2}", property.Name, property.Type, property.ComposeKey?"*":"");
                        else
                            methodNode.Text = "<New Property>";
                        methodNode.ImageIndex = 1;
                        methodNode.SelectedImageIndex = 1;

                        classNode.Nodes.Add(methodNode);
                    }
                }

                classNode.Expand();

                tvBE.Nodes.Add(classNode);
            }
        }

        private void InitializeView()
        {
            ImageList imageList = new ImageList();
            imageList.Images.Add(Properties.Resources.Class);
            imageList.Images.Add(Properties.Resources.Method);

            tvBE.ImageList = imageList;
        }

        private IAddNewBusinessEntity BEMain
        {
            get 
            {
                try
                {
                    return (IAddNewBusinessEntity)this.Parent.Parent.Parent;
                }
                catch { return null; }
            }
        }

        #region IAddNewBusinessEntity Members

        public BusinessEntity Entity
        {
            get
            {
                if (_entity == null && BEMain != null)
                    _entity = BEMain.Entity;

                return _entity;
            }
            set
            {
                _entity = value;
            }
        }

        public void RefreshEntity()
        {
            UpdateTreeView(this.Entity);
        }
        #endregion
    }
}
